package com.ophiux.cs.infrastructure.repositories.chat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ophiux.cs.domain.chat.model.entity.CSChatSession;
import com.ophiux.cs.domain.chat.repositories.ICSChatSessionRepository;
import com.ophiux.cs.domain.consult.model.entity.CSUser;
import com.ophiux.cs.infrastructure.mapper.chat.CSChatSessionMapper;
import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;

/**
 * 聊天消息仓储
 * @author chenh
 *
 */
@Repository("cSChatSessionRepository")
public class CSChatSessionRepository implements ICSChatSessionRepository{

	@Autowired
	private CSChatSessionMapper cSChatSessionMapper;
	
	@Override
	public void save(CSChatSession model) throws OphiuxRepositoryException {
		
		try {
			//判断用户是否存在，存在，更新；不存在，新增
			CSChatSession _csSession = cSChatSessionMapper.getCSChatSessionByID(model.getID());
			
			if( _csSession == null ) {
				//不存在，新增
				cSChatSessionMapper.insert(model);
			}else {
				//存在，更新用户
				cSChatSessionMapper.update(model);
			}
			
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("会话存储异常", e);
		}
		
	}

	@Override
	public CSChatSession getCSChatSession(String user, String who) throws OphiuxRepositoryException {
		
		try {
			return cSChatSessionMapper.getCSChatSession(user, who);
		} catch (OphiuxDaoException e) {
			throw new OphiuxRepositoryException("会话查询异常", e);
		}
		
	}

	
}
